package com.icss.th.question.facade.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.icss.th.common.exception.ThException;
import com.icss.th.common.util.StringUtil;
import com.icss.th.db.DBConnection;
import com.icss.th.db.sql.SqlText;
import com.icss.th.question.facade.IQuestionFacade;
import com.icss.th.question.pojo.Item;
import com.icss.th.question.pojo.Question;

public class QuestionFacadeMysql implements IQuestionFacade 
{	
	@Override
	public String getBrotherId(String paperid, String sort, boolean next) throws ThException 
	{
		String id = null;
		
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try
		{
			try
			{
				ps = conn.prepareStatement(next? SqlText.GET_NEXT_ID : SqlText.GET_PREVIOUS_ID);
				ps.setInt(1, Integer.valueOf(paperid));
				ps.setInt(2, Integer.valueOf(sort));
				rs = ps.executeQuery();
				if (rs.next())	id = String.valueOf(rs.getInt("ExamID"));
			}
			finally
			{
				rs.close();
				ps.close();
			}
		}
		catch (SQLException e)
		{
			throw new ThException(e.getMessage(), e);
		}
		
		return id;
	}

	@Override
	public Question getQuestion(String id) throws ThException
	{
		Question question = null;
		
		Connection conn = DBConnection.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try
		{
			try
			{
				ps = conn.prepareStatement(SqlText.GET_QUESTION_SQL);
				ps.setString(1, id);
				rs = ps.executeQuery();
				if (rs.next())
				{
					int examId = rs.getInt("ExamID");
					String examType = rs.getString("examType");
					String examTitle = rs.getString("examTitle");
					String A = rs.getString("A");
					String B = rs.getString("B");
					String C = rs.getString("C");
					String D = rs.getString("D");
					String E = rs.getString("E");
					String F = rs.getString("F");
					String G = rs.getString("G");
					String pageUrl = rs.getString("PageUrl");
					String examAnswer = rs.getString("examAnswer");
					String examExplain = rs.getString("examExplain");
					int inPaperId = rs.getInt("inPaperId");
					String paperTitle = rs.getString("paperTitle");
					int paperSort = rs.getInt("inPaperSort");
					int keyType = rs.getInt("keyType");
					
					List<Item> items = new ArrayList<Item>();
					if (!StringUtil.isEmpty(A))	items.add(new Item("A", StringUtil.format(A)));
					if (!StringUtil.isEmpty(B))	items.add(new Item("B", StringUtil.format(B)));
					if (!StringUtil.isEmpty(C))	items.add(new Item("C", StringUtil.format(C)));
					if (!StringUtil.isEmpty(D))	items.add(new Item("D", StringUtil.format(D)));
					if (!StringUtil.isEmpty(E))	items.add(new Item("E", StringUtil.format(E)));
					if (!StringUtil.isEmpty(F))	items.add(new Item("F", StringUtil.format(F)));
					if (!StringUtil.isEmpty(G))	items.add(new Item("G", StringUtil.format(G)));
					
					question = new Question();
					question.setId(examId);
					question.setTitle(StringUtil.format(examTitle));
					question.setAnswer(StringUtil.format(examAnswer));
					question.setExplain(StringUtil.format(examExplain));
					question.setItems(items);
					question.setPaperName(StringUtil.format(paperTitle));
					question.setPaperId(inPaperId);
					question.setSourceUrl(StringUtil.format(pageUrl));
					question.setTypeName(StringUtil.format(examType));
					question.setPaperSort(paperSort);
					question.setTypeId(keyType);
				}
			}
			finally
			{
				rs.close();
				ps.close();
			}
		}
		catch (Exception e)
		{
			throw new ThException(e.getMessage(), e);
		}
		
		return question;
	}
}
